package com.yimixian.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.ta.utdid2.android.utils.StringUtils;
import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.cart.CartManager;
import com.yimixian.app.cart.GoodsItemPresenter;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.StatConstants;
import com.yimixian.app.common.WebViewActivity;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.goods.GoodDetailActivity;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.ui.PlusMinusIcon;
import com.yimixian.app.util.Strings;
import com.ymx.sdk.util.DensityUtils;
import com.ymx.sdk.util.DeviceUtils;
import com.ymx.sdk.widget.recyclerview.HFListRecyclerAdapter;
import com.ymx.sdk.widget.recyclerview.viewholder.AbsViewHolder;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by zhilian-2 on 2015/10/16.
 */
public class SessionRecyclerViewAdapter extends HFListRecyclerAdapter<GoodsItem>{

    private Context context;
    private GoodsItemPresenter.OnPlusButtonClickListener mOnPlusButtonClickListener;

    public SessionRecyclerViewAdapter(ArrayList<GoodsItem> datas, Context context,
                                      GoodsItemPresenter.OnPlusButtonClickListener plusButtonClickListener) {
        super(datas, context);
        this.context = context;
        this.mOnPlusButtonClickListener = plusButtonClickListener;
    }

    @Override
    public int getItemLayoutResId() {
        return R.layout.session_item;
    }

    @Override
    public AbsViewHolder getItemViewHoler(ViewGroup parent, int viewType) {
        ItemHolder itemHolder = new ItemHolder(getItemView(parent, viewType), mContext);
        itemHolder.setOnPlusButtonClickListener(mOnPlusButtonClickListener);
        return itemHolder;
    }

    class ItemHolder extends AbsViewHolder{

        @InjectView(R.id.list_item)
        RelativeLayout listItem;
        @InjectView(R.id.goods_image)
        ImageView mGoodsImage;
        @InjectView(R.id.good_name)
        TextView mTitleText;
        @InjectView(R.id.rest_count)
        TextView leftCount;
        @InjectView(R.id.once_count)
        TextView onceCount;
        @InjectView(R.id.start_sale)
        TextView startSale;
        @InjectView(R.id.image_top_left)
        ImageView mPromotionImage;
        @InjectView(R.id.lin_right_top)
        LinearLayout linRightTop;
        @InjectView(R.id.buy_price_text)
        TextView buyPriceText;
        @InjectView(R.id.old_price_text)
        TextView oldPriceText;
        @InjectView(R.id.control_area)
        FrameLayout mControlArea;
        @InjectView(R.id.tuan_button)
        TextView mTuanButton;
        @InjectView(R.id.control_tuan_area)
        FrameLayout mControlTuanArea;

        private GoodsItem mGoodsItem;
        private GoodsItemPresenter.OnPlusButtonClickListener mOnPlusButtonClickListener;
        private PlusMinusIcon mPlusMinusIcon;
        private Picasso picasso;
        private int mScreenWidht = 0;
        private float height = 0;

        public ItemHolder(View itemView, Context context) {
            super(itemView, context);
            ButterKnife.inject(this, itemView);
            mScreenWidht = DeviceUtils.getScreenWidth(context);
            picasso = Picasso.with(context);

        }

        @Override
        public void bindItemData(Object data) {
            bindData((GoodsItem) data);
        }

        public void setOnPlusButtonClickListener(GoodsItemPresenter.OnPlusButtonClickListener listener) {
            mOnPlusButtonClickListener = listener;
        }

        public void bindData(final GoodsItem goodsItem) {
            if (goodsItem == null) {
                mGoodsItem = null;
                return;
            }

            int count = 0;
            if(CartManager.getInstance().mCartGoodsItems != null && goodsItem != null) {
                count = CartManager.getInstance().mCartGoodsItems.get(goodsItem.id) == null ? 0 : Integer.parseInt(CartManager.getInstance().mCartGoodsItems.get(goodsItem.id).count);
            }
            if (mPlusMinusIcon == null) {
                mPlusMinusIcon = new PlusMinusIcon(SystemFramework.getInstance().getGlobalContext(), goodsItem, Constants.STATE_SESSIONLIST_BUTTON_BROADCAST);
                mControlArea.addView(mPlusMinusIcon);
                mPlusMinusIcon.setQuantity(count);
                mPlusMinusIcon.setOnPlusButtonClickListener(mOnPlusButtonClickListener);
            } else {
                mPlusMinusIcon.updateGoodsItem(goodsItem);
                mPlusMinusIcon.setQuantity(count);
            }

            mGoodsItem = goodsItem;

            if (height == 0){
                height = mGoodsItem.imgRatio == 0 ? DensityUtils.dp2px(mContext, 125) :
                        mScreenWidht / mGoodsItem.imgRatio;
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, (int) height);
                listItem.setLayoutParams(params);
            }
            picasso.load(DataManager.getInstance().getImageUrl(goodsItem.img, mScreenWidht / 2))
                    .placeholder(R.drawable.image_left_default)
                    .into(mGoodsImage);
            if (Strings.isNullOrEmpty(mGoodsItem.promotionImg)) {
                mPromotionImage.setVisibility(View.GONE);
            } else {
                mPromotionImage.setVisibility(View.VISIBLE);
                int size = mContext.getResources().getDimensionPixelSize(R.dimen.promotion_image_size);
                picasso.load(DataManager.getInstance().getImageUrl(mGoodsItem.promotionImg, size)).into(
                        mPromotionImage);
            }

            // 判断是否为团购商品
            mTuanButton.setText(context.getResources().
                    getString(R.string.group_buying));
            if (goodsItem.isBuyingGroup == 1) {
                mControlArea.setVisibility(View.GONE);
                mControlTuanArea.setVisibility(View.VISIBLE);
                if (goodsItem.shouldBuyCount < 1) {
                    mTuanButton.setBackgroundResource(R.drawable.rounded_button_disabled);
                    mTuanButton.setEnabled(false);
                }else {
                    mTuanButton.setBackgroundResource(R.drawable.orange_button_bg);
                    mTuanButton.setEnabled(true);
                }
            } else {
                mControlTuanArea.setVisibility(View.GONE);
                mControlArea.setVisibility(View.VISIBLE);
            }

            mTuanButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startTuanWebActivity(goodsItem);
                }
            });

            mTitleText.setText(goodsItem.name);

            String buyingGroupCount = goodsItem.isBuyingGroup == 1 ? goodsItem.buyingGroupCount + "人团  " : "";
            if (goodsItem.isBuyingGroup == 1){
                onceCount.setText(buyingGroupCount +  "  " +
                        (StringUtils.isEmpty(goodsItem.unitDesc) ? "" : goodsItem.unitDesc));
            }else{
                onceCount.setText(StringUtils.isEmpty(goodsItem.unitDesc) ? "" : goodsItem.unitDesc);
            }

            leftCount.setText(StringUtils.isEmpty(goodsItem.stockCount) ? "" : goodsItem.stockCount);
            startSale.setText(StringUtils.isEmpty(goodsItem.saleStatus) ? "" : goodsItem.saleStatus);
            buyPriceText.setText(StringUtils.isEmpty(goodsItem.unitPrice) ? "" : goodsItem.unitPrice);
            oldPriceText.setText(StringUtils.isEmpty(goodsItem.unitOldPrice) ? "" : goodsItem.unitOldPrice);
            oldPriceText.setPaintFlags(oldPriceText.getPaintFlags()
                    | Paint.STRIKE_THRU_TEXT_FLAG);
        }

        private void startTuanWebActivity(GoodsItem goodsItem) {
            Intent intent = new Intent(mContext, WebViewActivity.class);
            intent.putExtra(WebViewActivity.EXTRA_URL, goodsItem.descUrl);
            mContext.startActivity(intent);
        }
    }
}
